﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Configuration;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
using System.Web.UI.WebControls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
public partial class partner_paretner_notes : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["email"] == null)
        {
            Response.Redirect("sessiont.aspx");
        }
        gvUserInfo.Visible = true;
        Label2.Text = Session["email"].ToString();

        //if(Page.IsPostBack )
        //{
        //}
        //else{
        SqlConnection conn = null;
        conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString);
        conn.Open();
        SqlCommand scmd = new SqlCommand("SELECT * FROM client_add1 where c_id='" + DropDownList1.SelectedValue + "'", conn);

        SqlDataReader dr = scmd.ExecuteReader();
        if (dr.Read())
        {

            //txtcreate.Text = System.DateTime.Now();
            txtagent.Text = Session["email"].ToString();





        }


        dr.Close();

    }






    protected void Save_Click(object sender, System.EventArgs e)
    {
        if (FileUpload1.HasFile)
        {

            string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);

            string fileExtension = Path.GetExtension(FileUpload1.PostedFile.FileName);

            string documentType = string.Empty;



            //provide document type based on it's extension

            switch (fileExtension)
            {

                case ".pdf":

                    documentType = "application/pdf";

                    break;

                case ".xls":

                    documentType = "application/vnd.ms-excel";

                    break;

                case ".xlsx":

                    documentType = "application/vnd.ms-excel";

                    break;

                case ".doc":

                    documentType = "application/vnd.ms-word";

                    break;

                case ".docx":

                    documentType = "application/vnd.ms-word";

                    break;

                case ".gif":

                    documentType = "image/gif";

                    break;

                case ".png":

                    documentType = "image/png";

                    break;

                case ".jpg":

                    documentType = "image/jpg";

                    break;

            }



            //Calculate size of file to be uploaded

            int fileSize = FileUpload1.PostedFile.ContentLength;



            //Create array and read the file into it

            byte[] documentBinary = new byte[fileSize];

            FileUpload1.PostedFile.InputStream.Read(documentBinary, 0, fileSize);
            SqlConnection conn = null;
            conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString);
            conn.Open();
            SqlCommand cmd = new SqlCommand("insert into task values(@action_,@c_id ,@added_by, @c_name,@to_do ,@created ,@updated,@fast_reminder,@agent,@sec_agent,@rank_,@commission,@refer,@comment,@DocName,@Type_,@DocData)", conn);


            cmd.Parameters.AddWithValue("@action_", DropDownList5.SelectedValue);
            cmd.Parameters.AddWithValue("@c_id", DropDownList1.SelectedValue);
            cmd.Parameters.AddWithValue("@added_by", Session["email"]);

            cmd.Parameters.AddWithValue("@c_name", DropDownList3.SelectedValue);
            cmd.Parameters.AddWithValue("@to_do", DropDownList4.SelectedValue);
            cmd.Parameters.AddWithValue("@created", txtcreate.Text);
            cmd.Parameters.AddWithValue("@updated", txtupdate.Text);
            cmd.Parameters.AddWithValue("@fast_reminder", txtreminder.Text);
            cmd.Parameters.AddWithValue("@agent", txtagent.Text);
            cmd.Parameters.AddWithValue("@sec_agent", DropDownList6.SelectedValue);
            cmd.Parameters.AddWithValue("@rank_", txtrank.Text);
            cmd.Parameters.AddWithValue("@commission", txtagent.Text);
            cmd.Parameters.AddWithValue("@refer", TextBox2.Text);
            cmd.Parameters.AddWithValue("@comment", txtcomment.Text);
            SqlParameter DocName = new SqlParameter("@DocName", SqlDbType.VarChar, 50);

            DocName.Value = fileName.ToString();

            cmd.Parameters.Add(DocName);
            SqlParameter Type = new SqlParameter("@Type_", SqlDbType.VarChar, 50);

            Type.Value = documentType.ToString();

            cmd.Parameters.Add(Type);



            SqlParameter uploadedDocument = new SqlParameter("@DocData", SqlDbType.Binary, fileSize);

            uploadedDocument.Value = documentBinary;

            cmd.Parameters.Add(uploadedDocument);



            cmd.ExecuteNonQuery();
            Response.Redirect("partner_notes_conf.aspx");
            //Label1.Text = Session["email_id"].ToString();


        }

    }

    protected void gvUserInfo_RowCommand(object sender, GridViewCommandEventArgs e)
    {

        if (e.CommandName == "action")
        {

            //Label1.Text = "hello";
            int index = Convert.ToSByte(e.CommandArgument);

            GridViewRow row = gvUserInfo.Rows[index];
            SqlDataAdapter adp = new SqlDataAdapter();
            SqlCommand cmd1 = new SqlCommand();
            DataSet ds = new DataSet();


            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Connstr"].ConnectionString);

            con.Open();

            SqlCommand objectvalue = new SqlCommand("", con);
            cmd1.Connection = con;



            SqlCommand dmc = new SqlCommand("insert into notes(c_id,f_name,notes,date_) values ('" + row.Cells[0].Text + "','" + row.Cells[1].Text + "','" + DropDownList5.SelectedValue + "','" + System.DateTime.Now + "')", con);

            dmc.ExecuteNonQuery();
            //cmd.Parameters.AddWithValue("@notes", DropDownList2.SelectedValue);

            con.Close();

            Response.Write(@"<script language='javascript'>alert('clent has been selected')</script>");

            // con.Close();


        }
    }



}
